#include "PagePreprocess/FileProcessor.h"
#include <regex>

using namespace tinyxml2;

FileProcessor::FileProcessor()
:_webid(0)
{ 
}

void FileProcessor::process(const string &filename)
{

    XMLDocument doc;
    WebPage wbp;
    doc.LoadFile(filename.c_str());
    if (doc.ErrorID())
    {
        LogError("加载cml失败");
    }
    XMLElement *itemNode = doc.FirstChildElement("rss")->FirstChildElement("channel")->FirstChildElement("item");
    while(itemNode)
    {
        if (itemNode->FirstChildElement("title"))
        {
            wbp._title = itemNode->FirstChildElement("title")->GetText();
        }
        if (itemNode->FirstChildElement("link"))
        {
            wbp._link = itemNode->FirstChildElement("link")->GetText();
        }
        if (itemNode->FirstChildElement("description"))
        {
            wbp._description = itemNode->FirstChildElement("description")->GetText();
        }
        if (itemNode->FirstChildElement("content"))
        {
            wbp._description += itemNode->FirstChildElement("content")->GetText();
        }

        wbp._id = _webid;
        ++_webid;
        
        std::regex reg("<[^>]+>");
        wbp._description = regex_replace(wbp._description, reg, "");
        _webpages.push_back(wbp);

        // LogDebug("id:%d", wbp._id);
        // LogDebug("标题：%s", wbp._title.c_str());
        // LogDebug("链接：%s", wbp._link.c_str());
        // LogDebug("描述：%s", wbp._description.c_str());

        itemNode = itemNode->NextSiblingElement("item");
    }
}

vector<WebPage> &FileProcessor::getWebPages()
{   
    return _webpages;
}
void FileProcessor::clearwebpages()
{
    _webpages.clear();
}